package com.example.ui.contact;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.json.JSONException;
import org.json.JSONObject;

import com.example.huashangapp.HSApplication;
import com.example.huashangapp.R;
import com.example.ui.contact.adapter.NoticeAdapter;
import com.example.ui.contact.bean.MeItem;
import com.example.ui.contact.bean.NoticeItem;
import com.example.ui.contact.bean.PraiseItem;
import com.example.ui.contact.chat.L;
import com.example.ui.contact.chat.PushMessageReceiver;
import com.example.ui.contact.chat.PushMessageReceiver.EventHandler;
import com.example.ui.contact.db.NoticeMsgDB;
import com.example.ui.leftmenu.Item07View;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;


public class NoticeActivity extends Activity implements EventHandler{
	
	private Button titleLeftBtn,titleRightBtn;	
	private TextView titleText;	
	private ListView listview;
	private LinearLayout noticeLayout;
	private List<Map<String, Object>> listData;
	private Intent intent;
	private String value;
	private List<NoticeItem> msgList = new ArrayList<NoticeItem>();// 消息对象数组
	public static final int NEW_MESSAGE = 0x001;// 收到消息
	private NoticeMsgDB noticeMsgDB;
	private NoticeAdapter adapter;
	private Context mContext;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		mContext = this;
		setContentView(R.layout.chat_notice);
		intent=getIntent();
		value=intent.getStringExtra("text");
		noticeMsgDB = HSApplication.getInstance().getNoticeMsgDB();	
		initMeMsgData();
		findView();
		listView();
	}
	
	/**
	 * 从数据库中读出与我相关--系统通知历史数据
	 */
	public void initMeMsgData() {
		MsgState.state=MsgState.NOTICE_MSG_STATE;
		List<NoticeItem> list = noticeMsgDB.getNoticeMsgList();  //getMsg(mFromUser.getUserId(),MsgPagerNum);
		if (list.size() > 0) {
			for (NoticeItem entity : list) {

				msgList.add(entity);
			}
		}

	}
	
	private void findView() {
		titleLeftBtn = (Button)findViewById(R.id.layout_title_left_btn);
		titleRightBtn= (Button)findViewById(R.id.layout_title_right_btn);
		titleText=(TextView)findViewById(R.id.layout_title_middle_text);
		listview=(ListView)findViewById(R.id.chat_notice_listview);
		noticeLayout=(LinearLayout)findViewById(R.id.chat_notice_none_layout);
		
		initView();
	}

	public void initView(){
		titleLeftBtn.setText("返回");
		titleText.setText(value);
		titleRightBtn.setVisibility(View.INVISIBLE);
		titleLeftBtn.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				Item07View.my_adapter.notifyDataSetChanged();
				finish();
			}
		});
	}
	@SuppressWarnings("static-access")
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		
		if(keyCode== event.KEYCODE_BACK){
			Item07View.my_adapter.notifyDataSetChanged();
			finish();			
		}
		return true;
	}
	private void listView() {
		getNoticeData();
		adapter = new NoticeAdapter(mContext,noticeLayout, msgList);
		listview.setAdapter(adapter);
		
	}
	
	class Holder {
		TextView content;
		TextView time;
	}

	
	/**
	 * 获得系统通知的数据
	 */
	private void getNoticeData() {
		listData=new ArrayList<Map<String,Object>>();
		//在这里获得系统通知的数据，然后添加到listData里面
		//、、、、、、、、、、、、、、、
	}

	/**
	 * 处理接收过来的消息
	 */
	private Handler handler = new Handler() {
		public void handleMessage(android.os.Message msg) {
			
			if (msg.what == NEW_MESSAGE) {
				String message = (String) msg.obj;
				if (message.length() > 0)
				{					
					MsgHandle(message);
				}		
				

			}
		}
	};
	
	public void MsgHandle(String message){
		try {

			String MeContent = new JSONObject(message.toString()).getString("systemNotifications");
			JSONObject json = new JSONObject(MeContent);
			NoticeItem item = new NoticeItem();					
			item.setContent(json.getString("content"));
			item.setDateLine(json.getString("time"));				
			//HSApplication.getInstance().getNoticeMsgDB().saveNoticeMsg(item);
			adapter.updateMsg(item);
			L.Debug("NoticeActivity notice run ok","");
				



		} catch (JSONException e) {
			e.printStackTrace();
		}		
	}
	@Override
	public void onMessage(String message) {
		L.Debug("NoticeActivity onMessage", "");
		try {
			String type = new JSONObject(message.toString()).getString("response").trim().toString();
			if(type.equals("systemNotifications")){
				Message handlerMsg = handler.obtainMessage(NEW_MESSAGE);
				handlerMsg.obj = message;
				handler.sendMessage(handlerMsg);	
			}
		} catch (JSONException e) {
			L.Debug("NoticeActivity error", e.getMessage());
			e.printStackTrace();
		}

		
	}
	@Override
	public void onBind(String method, int errorCode, String content) {
		
	}

	@Override
	public void onNotify(String title, String content) {
		
	}

	@Override
	public void onNetChange(boolean isNetConnected) {
		
	}
	@Override
	protected void onPause() {
		super.onPause();
		PushMessageReceiver.ehList_Notice.remove(this);//移除监听
	}	
	@Override
	protected void onResume() {
		super.onResume();
		PushMessageReceiver.ehList_Notice.add(this);// 监听推送的消息
	}
	

}
